import os

from mitmproxy import http

# 获取到当前目录的路径再与要读取的路径进行拼接
curl_path = os.path.dirname(os.path.realpath(__file__))
path = os.sep.join([curl_path,"./quote.json"])


def request(flow: http.HTTPFlow):
    # 先判断要mock的接口url
    if "quote.json" in flow.request.pretty_url and "x=" in flow.request.pretty_url:
        # 读取要替换的内容
        with open(path,encoding='utf-8') as f:
            # 将返回内容替换成需要的数据
            flow.response = http.Response.make(
                200,
                f.read(),
                {"Content-Type":"application/json"}
            )
